package com.netty.server.console;

import utl.mofen.jsonStreamHandler.HandlerFactory;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.socket.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class NettyServer {
	
    private final int port;

    public NettyServer(int port) {
        this.port = port;
    }

    public void run() throws Exception {
    	
    	ServerBootstrap b = new ServerBootstrap();
        try {       
            b.group(new NioEventLoopGroup(), new NioEventLoopGroup());
            b.channel(NioServerSocketChannel.class);
            b.localAddress(port);
            b.childHandler(new NettyServerInitializer());

            b.bind().sync().channel().closeFuture().sync();
        } finally {
            b.shutdown();
        }
    }

    public static void main(String[] args) throws Exception {
        int port;
        if (args.length > 0) {
            port = Integer.parseInt(args[0]);
        } else {
            port = 8080;
        }
        
        HandlerFactory.checkInitialize(null);
        
        new NettyServer(port).run();
    }
}
